package com.fir.my.batis.plus.until;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public  static  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public  static  SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

    public static String getCurrentTime(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        return df.format(new Date());
    }

    public static Calendar getC(){
        Calendar c = Calendar.getInstance();
        return c;
    }
    public static Date  getcutMinutes(Date date,Integer min_cut){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE,min_cut);
        return c.getTime();
    }
    public static Calendar getC(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }
    public static String getMStr(int month){

        if(month < 10){
            return "0"+month;
        }else{

        }
        return  String.valueOf(month);
    }
    public static String getBc(){
        int hour = DateUtil.getC().get(Calendar.HOUR_OF_DAY);
        if(hour > 6 || hour < 19){
            return "白班";
        }
        return "夜班";
    }
    public static String getBc(Date date){
        int hour = DateUtil.getC(date).get(Calendar.HOUR_OF_DAY);
        if(hour > 6 || hour < 19){
            return "白班";
        }
        return "夜班";
    }
    /**

     * 天加减 和 定位几点 （具体几点）
     * @param day_cut
     *   * **************************重要方法*****************
     */
    public static Date  getDayTimeOfCut(Integer hourPoit,Integer day_cut){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,hourPoit);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        calendar.add(Calendar.DAY_OF_YEAR,day_cut);
        Date date = calendar.getTime();
        return date;
    }
    public static Date  getCutDay(Integer day_cut){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR,day_cut);
        Date date = calendar.getTime();
        return date;
    }
    public static Date  getDayOfCut(Date date,Integer day_cut){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,day_cut);
        return calendar.getTime();
    }
    /**
     * 衍生
     * 自getDayTimeOfCut 返回  格式化后的日期字符串 yyyy-MM-dd HH:mm:ss
     */
    public static String  getDayTimeOfCutForMat(Integer hourPoit,Integer day_cut){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(getDayTimeOfCut(hourPoit,day_cut));
    }
    /**
     * 衍生
     * 自getDayTimeOfCut 返回  格式化后的日期字符串 yyyy-MM-dd
     */
    public static String  getDayTimeOfCutForMatDay(Integer day_cut){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(getDayTimeOfCut(0,day_cut));
    }
    public static String  getForMatCutDay(Integer day_cut){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(getDayTimeOfCut(0,day_cut));
    }
    public static Date  getDayTimeOfCut(Integer hourPoit,Integer day_cut,Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,hourPoit);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        calendar.add(Calendar.DAY_OF_YEAR,day_cut);
        return calendar.getTime();
    }
    public static String  getDayTimeOfCutForMatDay(Integer day_cut,Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(getDayTimeOfCut(0,day_cut,date));
    }
    /**
     * 获取当天的日期 力度到小时 格式为
     * @param date
     */
    public static Date getCurrentDayTime(Date date,Integer hour){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        c1.set(Calendar.YEAR, Integer.valueOf(sdf.format(date).substring(0,4)));
        c1.set(Calendar.MONTH, Integer.valueOf(sdf.format(date).substring(5,7))-1);
        c1.set(Calendar.DAY_OF_MONTH, Integer.valueOf(sdf.format(date).substring(8,10)));
        c1.set(Calendar.HOUR_OF_DAY, hour);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        Date start = c1.getTime();
        return start;
    }
    public static String getCurrentDayTimeFormat(Date date,Integer hour){
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.set(Calendar.HOUR_OF_DAY, hour);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        Date start = c1.getTime();
        return sdf.format(start);
    }

    public static String getErpDate(String sd){
        return sd.substring(2).replaceAll("-",".");
    }

    /**
     * @param date
     * @param hour
     * @param date
     */
    public static Date getCurrentDayTimeFormatDate(Date date,Integer hour,SimpleDateFormat sdf){
        if(sdf == null){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date);
        c1.set(Calendar.HOUR_OF_DAY, hour);
        c1.set(Calendar.MINUTE, 0);
        c1.set(Calendar.SECOND, 0);
        Date start = c1.getTime();
        return start;
    }
    public static String getCurrentDayFormat(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 获取 erp日期 示例： 22.04.26
     * @param date
     * @return
     */
    public static String getDayYearMounthDay(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(date);
        return   s.split("-")[0].substring(2,4)+"."+s.split("-")[1]+"."+s.split("-")[2];
    }
    /**
     * 加强版的加减时间和固定小时
     * @param date 自定义时间 datetime
     * @param hour
     * @param day_cut
     */
    public static Date  getDayTimeOfCut(Date date,Integer hour,Integer day_cut){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,day_cut);
        calendar.set(Calendar.HOUR_OF_DAY,hour);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date date1 = calendar.getTime();
        return date1;
    }
    public static Date  getDayTimeOfCutForMonth(Date date,Integer hour,Integer month_cut){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH,month_cut);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date date1 = calendar.getTime();
        return date1;
    }

    /**
     * 当前时间加减小时  返回 datetime
     * @param hour_cut
     * @return
     */
    public static Date  getCurrentTimeByCutHour(Integer hour_cut){
        Calendar calendar0 = Calendar.getInstance();
        calendar0.add(Calendar.HOUR_OF_DAY, hour_cut);
       return  calendar0.getTime();
    }
    /**
     * 当前时间 加减小时  返回 yyyy-MM-dd HH:mm:ss
     * @param hour_cut
     * @return
     */
    public static String  getCurrentTimeByCutHourFormat(Integer hour_cut){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar0 = Calendar.getInstance();
        calendar0.add(Calendar.HOUR_OF_DAY, hour_cut);
       return  sdf.format(calendar0.getTime());
    }
    public static int  getCurentHour(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
    }
    public static String  getMonth00(int monthCut){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,monthCut);
        int month = calendar.get(Calendar.MONTH) + 1;
        if(month < 10){
            return "0" + month;
        }else{
            return month+"";
        }
    }
    public static int  getHour(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
    }
    public static String  pickClass(Date date){

        if(date == null){
            return "未知";//1是白班
        }else{
            int hour = DateUtil.getHour(date);//24小时制
            if(hour > 19 || hour < 7){
                return "夜班";//1是白班
            }else {
                return "白班";//1是白班
            }
        }
    }


    /**
     * 上个月的哪天
     * 相比今天   取  哪个月的哪天
     * @param monthCut 0是当月
     * @return 01 就是当月第一天
     */
    public static Date getDayByDayCUtAndMonthCut(int dayPoint,int monthCut,Integer poitHour){
        // 获取当月最后一天
        Calendar calendar =  Calendar.getInstance();
        calendar.add(Calendar.MONTH, monthCut);
        calendar.set(Calendar.DAY_OF_MONTH, dayPoint);
        if(null != poitHour){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    public static Date getDayByDayCUtAndMonthCutAndYear(int dayPoint,int monthCut,Integer poitHour,int cutYear){
        // 获取当月最后一天
        Calendar calendar =  Calendar.getInstance();
        calendar.add(Calendar.YEAR,cutYear);
        calendar.add(Calendar.MONTH, monthCut);
        calendar.set(Calendar.DAY_OF_MONTH, dayPoint);
        if(null != poitHour){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    public static Date getDayByDayCUtAndMonthCutYearCut(int dayPoint,int monthCut,Integer poitHour,int yearCut){
        // 获取当月最后一天
        Calendar calendar =  Calendar.getInstance();
        calendar.add(Calendar.MONTH, monthCut);
        calendar.add(Calendar.YEAR, yearCut);
        calendar.set(Calendar.DAY_OF_MONTH, dayPoint);
        if(null != poitHour){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    /**
     * 上个月的今天
     * @param date
     * @param monthCut
     * @return
     */
    public static Date getCurrentDayLastMonth(Date date,int monthCut,Integer poitHout){
        Calendar calendar = Calendar.getInstance();
        if(date != null){
            calendar.setTime(date);
        }
        int year = calendar.get(Calendar.YEAR);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);

        if(month  == 2 && day > 28){
            if(year % 4 == 0 && day > 29){
                calendar.set(Calendar.DAY_OF_MONTH,29); //
            }else{
                calendar.set(Calendar.DAY_OF_MONTH,28); //
            }
        }

        Integer[] ini = {4,6,11};
        if(day > 30){
            if(Arrays.asList(ini).contains(month)){
                if(day == 31){
                    calendar.set(Calendar.DAY_OF_MONTH,30); //
                }
            }
        }
        calendar.set(Calendar.HOUR_OF_DAY,0);
        if(poitHout != null){
            calendar.set(Calendar.HOUR_OF_DAY,poitHout);
        }
        calendar.add(Calendar.MONTH, monthCut); // 设置为上一个月
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();

    }
    /**
     *   去年的今天
     */
    public static Date getCurrentDayLastYear(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1); // 设置为上一个月
        return calendar.getTime();
    }

    /**
     * 哪年的 几号 （ 1,0） 是今天的 1月1号
     * 哪年的哪天
     * @param dayNum
     * @param yearCut
     * @return
     */
    public static Date getDayOfYear(int dayNum,int yearCut,Integer poitHour){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR,dayNum);//定点
        calendar.add(Calendar.YEAR, yearCut); //加减
        if(poitHour != null){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    /**
     * 哪年的今天
     * @param yearCut
     * @return
     */
    public static Date getCurrentDayOfYear(int yearCut,Integer poitHour){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + yearCut); // 设置为上一个月
        if(poitHour != null){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }
    public static Date getCurrentDayOfYear(Date date ,int yearCut,Integer poitHour){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if(poitHour != null){
            calendar.set(Calendar.HOUR_OF_DAY,poitHour);
        }
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + yearCut); // 设置为上一个月
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {
//        //年合格率环比
//        Date lastMonthEndDay =  DateUtilMy.getDayByDayCUtAndMonthCut(0,0,7);//上个月最后一天
//        System.out.println(sd.format(jnKsTime));
//        System.out.println(sd.format(lastMonthEndDay));
        //月吨损失
        Date currentMonthKsTime = DateUtil.getDayByDayCUtAndMonthCut(1,0,7);//月初第一天
        Date currentMonthEndTime =  DateUtil.getDayByDayCUtAndMonthCut(0,1,7);//这个月最后一天
        System.out.println(sd.format(currentMonthKsTime));
        System.out.println(sd.format(currentMonthEndTime));



    }
}
